<?php
	// Load the QCubed Development Framework
	require('includes/prepend.inc.php');
    //require_once('panel_search_tab.php');
    //require_once('panel_menubar.php');

	/**
	 * This is a quick-and-dirty draft QForm object to do Create, Edit, and Delete functionality
	 * of the TableUserProject class.  It uses the code-generated
	 * TableUserProjectMetaControl class, which has meta-methods to help with
	 * easily creating/defining controls to modify the fields of a TableUserProject columns.
	 *
	 * Any display customizations and presentation-tier logic can be implemented
	 * here by overriding existing or implementing new methods, properties and variables.
	 *
	 * NOTE: This file is overwritten on any code regenerations.  If you want to make
	 * permanent changes, it is STRONGLY RECOMMENDED to move both table_user_project_edit.php AND
	 * table_user_project_edit.tpl.php out of this Form Drafts directory.
	 *
	 * @package My Application
	 * @subpackage Drafts
	 */
	abstract class popup_base_jectly extends QForm {

        //panels
        //protected $pnl_search_tab;
        //protected $pnl_mebubar;
        //protected $pnl3;

        //mode of menubar
        //protected $mode_string_page;
        //protected $bool_mode_login=true;
        //protected $bool_mode_register=false;
        //protected $bool_mode_forgot=false;

        //protected $category_string;//query_string

        //protected $search_type_string;//query_string
        //protected $selected_category_index_string;//query_string

        //page variations control. these flags will be used for pages extending this class.
        //other flags will be added for different contents. it's easy to manage.
        //protected $bool_main_menu_on=true;
        //protected $bool_main_logo_on=true;
        //protected $bool_search_tab_on=true;
        //protected $bool_main_3boxes_on=true;
        //protected $bool_search_result_within_page_on=false;
        //protected $bool_breadcrumb_on=false;
        //protected $bool_account_menu_on=false;
        protected $bool_contents_area_on=true;

        protected $page_title;
        protected $page_description;
        protected $page_robot;

        protected $user=null;//TableUserProject
        protected $UserName;
        protected $UserId;
        //protected $bool_logined=false;

        protected $contents_html='<div class=\'center\'>This is a page.</div>';

		protected function Form_Run() {
			// Security check for ALLOW_REMOTE_ADMIN
			// To allow access REGARDLESS of ALLOW_REMOTE_ADMIN, simply remove the line below
			QApplication::CheckRemoteAdmin();
		}

		protected function Form_Create() {

            //$this->check_query_string();
            //$this->panel_search_tab_Create();
            //$this->panel_menubar_Create();

            /*if($this->bool_logined())
            {
                $this->pnl_mebubar->hide_all_controls();
            }

            if(isset($this->selected_category_index_string) && is_numeric($this->selected_category_index_string))
            {
                $this->set_index_listbox_search_project(intval($this->selected_category_index_string));
            }*/
		}

        protected function check_query_string()
        {
            //check menubar mode
            $this->mode_string_page=QApplication::QueryString('p');

            if(!empty($this->mode_string_page))
            {
                if($this->mode_string_page ==='0')
                {
                    $this->bool_mode_login=true;
                    $this->bool_mode_register=false;
                    $this->bool_mode_forgot=false;
                }
                else if($this->mode_string_page ==='1')
                {
                    $this->bool_mode_login=false;
                    $this->bool_mode_register=true;
                    $this->bool_mode_forgot=false;
                }
                else if($this->mode_string_page ==='2')
                {
                    $this->bool_mode_login=false;
                    $this->bool_mode_register=false;
                    $this->bool_mode_forgot=true;
                }
            }
            else
            {
                $this->bool_mode_login=true;
                $this->bool_mode_register=false;
                $this->bool_mode_forgot=false;
            }

            $this->category_string=QApplication::QueryString('c');//category_id

            $this->selected_category_index_string=QApplication::QueryString('i');//category_list_index

        }

        protected function bool_logined()
        {
            if (!isset($_SESSION['User']))return false;

            $objUser = unserialize($_SESSION['User']);

            if (!($objUser instanceof TableUserProject))return false;

            $this->UserName=$objUser->UserName;
            $this->UserId=$objUser->Id;

            //$this->pnl_mebubar->hide_all_controls();

            //$this->pnl_mebubar->textfield_login_username->Visible=false;
            //$this->pnl_mebubar->textfield_login_password->Visible=false;
            //$this->pnl_mebubar->button_login->Visible=false;

            return true;
        }

		/**
		 * This Form_Validate event handler allows you to specify any custom Form Validation rules.
		 * It will also Blink() on all invalid controls, as well as Focus() on the top-most invalid control.
		 */
		protected function Form_Validate() {
			// By default, we report that Custom Validations passed
			$blnToReturn = true;

			// Custom Validation Rules
			// TODO: Be sure to set $blnToReturn to false if any custom validation fails!

			/*$blnFocused = false;
			foreach ($this->GetErrorControls() as $objControl) {
				// Set Focus to the top-most invalid control
				if (!$blnFocused) {
					$objControl->Focus();
					$blnFocused = true;
				}

				// Blink on ALL invalid controls
				$objControl->Blink();
			}*/

			return $blnToReturn;
		}

		// Other Methods

        protected function RedirectToListPage() {
            //QApplication::Redirect(__VIRTUAL_DIRECTORY__ . '/index_2.php');
            QApplication::Redirect(QApplication::$RequestUri);
        }

        //actions
        public function login_Click($strFormId, $strControlId, $strParameter) {

            //QApplication::Redirect(QApplication::$RequestUri.'?login');

            //$this->pnl_mebubar->textfield_login_username
            //$this->pnl_mebubar->textfield_login_username->Text
            //$this->pnl_mebubar->textfield_login_password->Text

            /*$this->pnl_mebubar->label_message->Text='';

            $no_error_=false;
            $user_=false;

            $this->pnl_mebubar->textfield_login_username->Text=trim($this->pnl_mebubar->textfield_login_username->Text);
            $this->pnl_mebubar->textfield_login_password->Text=trim($this->pnl_mebubar->textfield_login_password->Text);

            //valdiate input
            if(strlen(trim($this->pnl_mebubar->textfield_login_username->Text))===0)
            {
                $this->pnl_mebubar->textfield_login_username->Focus();
                $this->pnl_mebubar->textfield_login_username->Blink();
                //$this->txtUserName->Warning=QApplication::Translate('Please input');
                $no_error_=false;
            }
            if(strlen(trim($this->pnl_mebubar->textfield_login_password->Text))===0)
            {
                $this->pnl_mebubar->textfield_login_password->Focus();
                $this->pnl_mebubar->textfield_login_password->Blink();
                //$this->txtEmailAddress->Warning=QApplication::Translate('Please input');
                $no_error_=false;
            }

            //validate username
            if(strlen($this->pnl_mebubar->textfield_login_username->Text)>30 || strlen($this->pnl_mebubar->textfield_login_username->Text)<4)
            {
                $this->pnl_mebubar->textfield_login_username->Focus();
                $this->pnl_mebubar->textfield_login_username->Blink();
                //$this->txtUserName->Warning=QApplication::Translate('Username is 6-30 characters');
                $no_error_=false;
            }
            else
            {
                if(TableUserProject::bool_exists_by_username($this->pnl_mebubar->textfield_login_username->Text))
                {
                    //$this->txtUserName->Warning=QApplication::Translate('The username is unavailable');
                    //$no_error_=false
                    $user_=TableUserProject::LoadByUserName($this->pnl_mebubar->textfield_login_username->Text);

                    if($user_->Password===$this->pnl_mebubar->textfield_login_password->Text)
                    {
                        //login ok
                        $_SESSION['User']=serialize($user_);
                        $_SESSION['Username']=$user_->UserName;
                        $user=$user_;
                        //$this->bool_logined=true;
                        $this->UserName=$user_->UserName;
                        $this->pnl_mebubar->label_message->Text=QApplication::Translate('Login OK');

                        $this->pnl_mebubar->textfield_login_username->Visible=false;
                        $this->pnl_mebubar->textfield_login_password->Visible=false;
                        $this->pnl_mebubar->button_login->Visible=false;
                    }
                    else
                    {
                        $this->pnl_mebubar->label_message->Text=QApplication::Translate('Username or password is wrong');
                        $no_error_=false;
                    }
                }
                else
                {
                    $this->pnl_mebubar->textfield_login_username->Focus();
                    $this->pnl_mebubar->textfield_login_username->Blink();

                    //$this->pnl_mebubar->label_message->Blink();

                    $this->pnl_mebubar->label_message->Text=QApplication::Translate('Username or password is wrong');
                    $no_error_=false;
                }
            }*/

        }

        function random_gen($length)
        {
            $chars = array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9));
            //$len = 16;
            $output = '';

            for ($x = 0; $x < $length; $x++)
            $output .= $chars[array_rand($chars)];

            return $output;
        }

        //page variations
        public function render_contents_area() {

            if($this->bool_contents_area_on)
            {

            }
        }



    }

?>